#import zipimport
#importer = zipimport.zipimporter('./networkx.mod')
#networkx = importer.load_module('networkx')

from networkx import  non_neighbors, is_connected, is_isomorphic

class GED1GroupGenerator:
    
    def __init__(self, thumbprintGenerator):
        self.thumbprintGenerator = thumbprintGenerator
        

    def setCodeGenerator(self, thumbprintGenerator):
        self.thumbprintGenerator = thumbprintGenerator
        
    def generateGED1Group(self, graph):
        group = []   
        addedGraphs=dict()
        initialThumbprint=self.thumbprintGenerator.generateThumbprint(graph)
        for node in graph.nodes():  
            for neighobor in graph.neighbors(node):
                for nonNeighbor in non_neighbors(graph, node):
                    newGraph = graph.copy()
                    newGraph.remove_edge(node, neighobor)
                    newGraph.add_edge(node, nonNeighbor)            
                    if is_connected(newGraph):
                        thumbprint = self.thumbprintGenerator.generateThumbprint(newGraph)
                        if (thumbprint != initialThumbprint) and (thumbprint not in addedGraphs):
                            addedGraphs[thumbprint] = len(group)
                            group.append(newGraph)       
        return group 
     
    
    def generateGED1GroupOld(self, graph):
        group = []   
        for node in graph.nodes():  
            for neighobor in graph.neighbors(node):
                for nonNeighbor in non_neighbors(graph, node):
                    newGraph = graph.copy()
                    newGraph.remove_edge(node, neighobor)
                    newGraph.add_edge(node, nonNeighbor)            
                    if is_connected(newGraph):
                        inGroup=False
                        if is_isomorphic(graph, newGraph):                            
                            continue;
                        for toTest in group:
                            if is_isomorphic(newGraph, toTest):    
                                inGroup = True
                                break
                        if not inGroup:
                            group.append(newGraph)
        return group